package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.tool.memcached.MemCachedConfig;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 充值优惠业务切面类
 * 
 * @Date:Created in 2018/7/3 14:17
 * @Author:
 * @Modified By: zjn 2018/7/3 14:17
 */ 
@Component
@Aspect
public class CustomerDepositGiftServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.CustomerDepositGiftService.findAll(..))")
    private void findAll(){}
    /**
    * 查询出数据返回，如果查询数据为null，则设置数据到缓存中
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/3  14:35
    */
    @Around("findAll()")
    public Object findAll(ProceedingJoinPoint joinPoint) throws Throwable {
        // "43:0"
        String key = CacheKey.K_ZERO_V_CUSTOMER_DEPOSIT_GIFT_LIST;
        // 根据key 获取缓存对象
        Object result = memCachedClient.get(key);
        if(result != null) {
            return result;
        }
        // 执行目标方法 findAll(..)
        result = joinPoint.proceed();
        if(result != null) {
            // 在缓存服务器上存储数据; 指定密钥，值和到期时间（一周）。
            memCachedClient.set(key, result, MemCachedConfig.CACHE_ONE_WEEK);
        }
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.CustomerDepositGiftService.update(..))")
    private void update() {}
    /**
    * 更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/3  14:36
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {

        String key = CacheKey.K_ZERO_V_CUSTOMER_DEPOSIT_GIFT_LIST;
        memCachedClient.delete(key);

        return joinPoint.proceed();
    }
}
